package org.jeecg.modules.demo.mall.yxuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.mall.entity.YxSystemUserLevel;
import org.jeecg.modules.demo.mall.entity.YxUser;
import org.jeecg.modules.demo.mall.entity.YxUserLevel;
import org.jeecg.modules.demo.mall.yxsystemuserlevel.service.IYxSystemUserLevelService;
import org.jeecg.modules.demo.mall.yxuser.mapper.YxUserMapper;
import org.jeecg.modules.demo.mall.yxuser.service.IYxUserService;
import org.jeecg.modules.demo.mall.yxuserlevel.service.IYxUserLevelService;
import org.jeecg.modules.demo.weixin.dto.PayDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @Description: yx_user
 * @Author: jeecg-boot
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Service
public class YxUserServiceImpl extends ServiceImpl<YxUserMapper, YxUser> implements IYxUserService {

    @Autowired
    private IYxSystemUserLevelService yxSystemUserLevelService;

    @Autowired
    private IYxUserLevelService yxUserLevelService;

    public void saveUserLevelValue(@RequestBody PayDto payDto) {

        YxUser user = this.getById(payDto.getUid());
        if (payDto.getGrowthValues() != null) {
            user.setUserGrowthValue(user.getUserGrowthValue().add(payDto.getGrowthValues()));
            this.updateById(user);

            //根据等级id获取等级信息
            YxSystemUserLevel userLevel = yxSystemUserLevelService.getById(user.getLevel());
            LambdaQueryWrapper<YxUserLevel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YxUserLevel::getOrderId, payDto.getOrderCouponNo());
            YxUserLevel userLevel1 = yxUserLevelService.getOne(queryWrapper);
            if (userLevel1 == null) {
                //添加充值记录
                YxUserLevel yxUserLevel = new YxUserLevel();
                yxUserLevel.setUid(user.getUid());
                yxUserLevel.setLevelId(user.getLevel());
                yxUserLevel.setGrade(userLevel.getGrade());
                yxUserLevel.setMerId(Integer.valueOf(payDto.getMerId()));
                yxUserLevel.setGrowthValue(payDto.getGrowthValues());
                yxUserLevel.setOrderId(payDto.getOrderCouponNo());
                yxUserLevelService.save(yxUserLevel);
            }
        }

    }

    @Override
    public String queryPhone(Integer uid) {
        YxUser byId = this.getById(uid);
        if (byId!=null){
            return byId.getPhone();
        }
        return null;
    }

}
